<template>
  <a-card :bordered="false">
    <a-col :md="4">
      <a-tree show-line @select="onSelect" :selectedKeys="selectedKeys">
        <a-icon slot="icon" type="carry-out" />
        <a-tree-node v-for="item in treeData" :key="item.id">
          <a-icon slot="icon" type="carry-out" />
          <span slot="title">{{item.mingCheng}}</span>
          <a-tree-node v-for="items in item.childList" :key="items.id">
            <a-icon slot="icon" type="carry-out" />
            <span slot="title">{{items.mingCheng}}</span>
            <a-tree-node v-for="itemss in items.childList" :key="itemss.id">
              <a-icon slot="icon" type="carry-out" />
              <span slot="title">{{itemss.mingCheng}}</span>
            </a-tree-node>
          </a-tree-node>
        </a-tree-node>
      </a-tree>
    </a-col>
    <a-col :md="20">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" :form="form">
          <a-row :gutter="24">
            <a-col :md="6">
              <a-form-item label="归档年度">
                <j-dict-select-tag
                  type="list"
                  v-decorator="['nianDu']"
                  dictCode="NianDu"
                  :trigger-change="true"
                  placeholder="请选择归档年度"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6">
              <a-form-item label="保管期限">
                <j-dict-select-tag
                  type="list"
                  v-decorator="['baoGuanQiXian']"
                  dictCode="BaoGuanQiXian"
                  :trigger-change="true"
                  placeholder="请选择保管期限"
                />
              </a-form-item>
            </a-col>
            <template v-if="toggleSearchStatus">
              <a-col :md="6">
                <a-form-item label="流转状态">
                  <j-dict-select-tag
                    type="list"
                    v-decorator="['liuZhuanZhuangTai']"
                    dictCode="liuZhuanZhuangTai"
                    :trigger-change="true"
                    placeholder="请选择流转状态"
                  />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleSubmit" icon="search">查询</a-button>
                <a-button
                  type="primary"
                  @click="searchReset"
                  icon="reload"
                  style="margin-left: 8px"
                >重置</a-button>
                <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
            <!-- <a-col :sm="24">
              <span style="float: right;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleSubmit" icon="search">查询</a-button>
                <a-button
                  type="primary"
                  @click="searchReset"
                  icon="reload"
                  style="margin-left: 8px"
                >重置</a-button>
              </span>
            </a-col>-->
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-tabs :activeKey="keys" @change="callback">
          <a-tab-pane :key="1" tab="卷">
            <a-button @click="scDangHao" type="primary">生成档号</a-button>
            <a-button @click="ruKu" type="primary">入库</a-button>
            <a-button @click="yiJiao" type="primary">移交</a-button>
            <a-button @click="unguiDang" type="primary">取消预归档</a-button>
            <a-button @click="daoChu" type="primary">导出</a-button>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
              <a style="margin-left: 24px" @click="ClearSelected">清空</a>
            </div>
            <a-table
              :columns="columns1"
              :data-source="listData"
              bordered
              rowKey="id"
              :loading="loading"
              :pagination="pagination"
              :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
              @change="handleTableChange"
            ></a-table>
          </a-tab-pane>
          <!-- <a-tab-pane :key="2" tab="件">
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
              <a style="margin-left: 24px" @click="ClearSelected">清空</a>
            </div>
            <a-table
              :columns="columns"
              :data-source="listData"
              bordered
              rowKey="id"
              :loading="loading"
              :pagination="pagination"
              :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
                   @change="handleTableChange"
            ></a-table>
          </a-tab-pane>-->
          <a-tab-pane :key="3" tab="盒">
            <a-button @click="scDangHao" type="primary">生成档号</a-button>
            <a-button @click="ruKu" type="primary">入库</a-button>
            <a-button @click="yiJiao" type="primary">移交</a-button>
            <a-button @click="unguiDang" type="primary">取消预归档</a-button>
            <a-button @click="daoChu" type="primary">导出</a-button>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
              <a style="margin-left: 24px" @click="ClearSelected">清空</a>
            </div>
            <a-table
              :columns="columns2"
              :data-source="listData"
              bordered
              rowKey="id"
              :loading="loading"
              :pagination="pagination"
              :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
              @change="handleTableChange"
            ></a-table>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-col>
  </a-card>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
import moment from 'moment'
export default {
  name: 'GuiDangList',
  components: {
    JDictSelectTag,
    JSelectDepart,
  },
  data() {
    return {
      // 树
      treeData: [],
      selectedKeys: [],
      mID: '',
      // tab
      keys: 1,
      // 列表
      listData: [],
      selectedRowKeys: [],
      loading: true,
      // 表头
      columns1: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 80,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '流转状态',
          align: 'center',
          dataIndex: 'liuZhuanZhuangTai_dictText',
        },
        {
          title: '目录号',
          align: 'center',
          dataIndex: 'muLuHao',
        },
        {
          title: '题名',
          align: 'center',
          dataIndex: 'tiMing',
        },
        {
          title: '保管期限',
          align: 'center',
          dataIndex: 'baoGuanQiXian_dictText',
        },
        {
          title: '归档年度',
          align: 'center',
          dataIndex: 'nianDu',
        },
      ],
      columns2: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 80,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '流转状态',
          align: 'center',
          dataIndex: 'liuZhuanZhuangTai_dictText',
        },
        {
          title: '目录号',
          align: 'center',
          dataIndex: 'muLuHao',
        },
        {
          title: '盒号',
          align: 'center',
          dataIndex: 'heHao',
        },
        {
          title: '保管期限',
          align: 'center',
          dataIndex: 'baoGuanQiXian_dictText',
        },
        {
          title: '归档年度',
          align: 'center',
          dataIndex: 'nianDu',
        },
      ],
      pagination: { pageSize: 10, current: 1, total: 0, showTotal: (total, a, b, c) => `总共 ${total} 条数据` },
      search: {},
      form: this.$form.createForm(this, { name: 'register' }),
      toggleSearchStatus: false,
    }
  },
  created() {
    getAction('/xtMenLei/tree', {}).then((res) => {
      if (res.code == 200) {
        let data = res.result
        let mkey = data[0].id
        this.treeData = data
        this.mID = mkey
        this.selectedKeys = mkey.split()
        getAction(`/daJuan/list?menLeiHangBiaoShi=${mkey}`, {}).then((res) => {
          if (res.code == 200) {
            this.listData = res.result.records
            this.loading = false
            const pagination = { ...this.pagination }
            pagination.total = res.result.total
            this.pagination = pagination
          }
        })
      }
    })
  },
  methods: {
    // 卷列表
    listJuan(values) {
      getAction(`/daJuan/list?menLeiHangBiaoShi=${this.mID}`, { ...values }).then((res) => {
        if (res.code == 200) {
          this.listData = res.result.records
          this.loading = false
          const pagination = { ...this.pagination }
          pagination.total = res.result.total
          this.pagination = pagination
        }
      })
    },
    // 件列表
    listJian(values) {
      getAction(`/daJian/list?menLeiHangBiaoShi=${this.mID}`, { ...values }).then((res) => {
        if (res.code == 200) {
          this.listData = res.result.records
          this.loading = false
          const pagination = { ...this.pagination }
          pagination.total = res.result.total
          this.pagination = pagination
        }
      })
    },
    // 盒列表
    listHe(values) {
      getAction(`/daHe/list?menLeiHangBiaoShi=${this.mID}`, { ...values }).then((res) => {
        if (res.code == 200) {
          this.listData = res.result.records
          this.loading = false
          const pagination = { ...this.pagination }
          pagination.total = res.result.total
          this.pagination = pagination
        }
      })
    },
    // 复选框
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // 清空
    ClearSelected() {
      this.selectedRowKeys = []
    },
    //  树操作
    onSelect(selectedKeys, info) {
      this.search = {}
      this.pagination.current = 1
      this.selectedKeys = selectedKeys
      this.form.resetFields()
      if (selectedKeys) {
        this.mID = selectedKeys.join()
        if (this.keys == 1) {
          this.listJuan()
        } else if (this.keys == 2) {
          this.listJian()
        } else if (this.keys == 3) {
          this.listHe()
        }
      }
    },
    // tab列表
    callback(key) {
      this.search.pageNo = 1
      this.pagination.current = 1
      this.selectedRowKeys = []
      this.keys = key
      if (key == 1) {
        this.listJuan(this.search)
      } else if (key == 2) {
        this.listJian(this.search)
      } else if (key == 3) {
        this.listHe(this.search)
      }
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      this.search.pageNo = pagination.current
      const pager = { ...this.pagination }
      pager.current = pagination.current
      this.pagination = pager
      if (this.keys == 1) {
        this.listJuan(this.search)
      } else if (this.keys == 2) {
        this.listJian(this.search)
      } else if (this.keys == 3) {
        this.listHe(this.search)
      }
    },
    // 查询
    handleSubmit(e) {
      this.form.validateFieldsAndScroll((err, values) => {
        if (err) {
          return
        }
        this.search = values
        this.search.pageNo = 1
        this.pagination.current = 1
        if (this.keys == 1) {
          this.listJuan(this.search)
        } else if (this.keys == 2) {
          this.listJian(this.search)
        } else if (this.keys == 3) {
          this.listHe(this.search)
        }
      })
    },
    // 重置
    searchReset() {
      this.search = {}
      this.search.pageNo = 1
      this.pagination.current = 1
      this.form.resetFields()
      if (this.keys == 1) {
        this.listJuan()
      } else if (this.keys == 2) {
        this.listJian()
      } else if (this.keys == 3) {
        this.listHe()
      }
    },
    handleToggleSearch() {
      this.toggleSearchStatus = !this.toggleSearchStatus
    },
    // 导出
    daoChu() {
      if (this.selectedRowKeys.length > 0) {
        this.search.ids = this.selectedRowKeys.join()
      }
      let paramsStr = encodeURI(JSON.stringify(this.search))
      let url = `${window._CONFIG['domianURL']}/${11111111111}?paramsStr=${paramsStr}`
      window.location.href = url
    },
    // 生成档号
    scDangHao() {
      if (this.selectedRowKeys.length > 0) {
        let ids = this.selectedRowKeys.join()
        if (this.keys == 1) {
          postAction('/daJuan/recordNumber', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listJuan()
            } else {
              this.$message.error(res.message)
            }
          })
        } else if (this.keys == 2) {
          this.$message.error('件类型不可以生成档号!')
        } else if (this.keys == 3) {
          postAction('/daHe/recordNumber', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listHe()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      }
    },
    // 入库
    ruKu() {
      if (this.selectedRowKeys.length > 0) {
        let ids = this.selectedRowKeys.join()
        if (this.keys == 1) {
          postAction('/daJuan/storage', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listJuan()
            } else {
              this.$message.error(res.message)
            }
          })
        } else if (this.keys == 2) {
          this.$message.error('件类型不可以入库!')
        } else if (this.keys == 3) {
          postAction('/daHe/storage', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listHe()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      }
    },
    // 移交
    yiJiao() {
      if (this.selectedRowKeys.length > 0) {
        let ids = this.selectedRowKeys.join()
        if (this.keys == 1) {
          postAction('/daJuan/transition', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listJuan()
            } else {
              this.$message.error(res.message)
            }
          })
        } else if (this.keys == 2) {
          this.$message.error('件类型不可以移交!')
        } else if (this.keys == 3) {
          postAction('/daHe/transition', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listHe()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      }
    },
    // 取消归档
    unguiDang() {
      if (this.selectedRowKeys.length > 0) {
        let ids = this.selectedRowKeys.join()
        if (this.keys == 1) {
          postAction('/daJuan/cancelPreArchive', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listJuan()
            } else {
              this.$message.error(res.message)
            }
          })
        } else if (this.keys == 2) {
          this.$message.error('件类型不可以取消预归档!')
        } else if (this.keys == 3) {
          postAction('/daHe/cancelPreArchive', { ids }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.listHe()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      } else {
        this.$message.error('请选择要取消预归档的数据!')
      }
    },
  },
}
</script>

<style lang="less">
</style>